//
//  JiuDianCell.m
//  HubeiHotel
//
//  Created by LiBenCan on 17/11/20.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "JiuDianCell.h"

@implementation JiuDianCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    //    继承父类
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = 0;
    }
    return self;
}

-(void)loadCellWithData:(NSDictionary *)data
{
    //    重新布局
    for (UIView *view in self.contentView.subviews) {
        [view removeFromSuperview];
    }
    
    UIView *baseView = [UIView new];
    baseView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:baseView];
    [baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(0*screenW_6);
        make.top.equalTo(self.contentView).offset(0*screenW_6);
        make.width.equalTo(@(SCREENW));
        make.height.equalTo(@(150*screenW_6));
        make.bottom.equalTo(self.contentView.mas_bottom).offset(0*screenW_6);
    }];

    NSString *title = [NSString stringWithFormat:@"【%@】",data[@"name"]];
    NSString *adress = [NSString stringWithFormat:@"%@(%.1lfkm)",data[@"tradingArea"][@"name"],[data[@"distance"] floatValue]];
    NSString *money = [NSString stringWithFormat:@"￥%@起",data[@"consumeMoney"]];

    UIImageView *imageV = [TomamaUI createImageView:CGRectMake(15*screenW_6, 20*screenW_6, 107.5*screenW_6, 120*screenW_6) backGroundImageV:@""];
    [baseView addSubview:imageV];
    imageV.image = [UIImage imageNamed:@"Main_hotel"];
    
    NSString * imageUrl = [data[@"image"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    UIImage *newImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:imageUrl];//用地址去本地找图片
    if (newImage != nil) {//如果本地有
        // newImage就是图片
        imageV.image = newImage;
        
    } else {//如果本地没有
        //下载图片
        [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:imageUrl] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            //这里是处理下载进度的,好像没必要管他
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
            if (image) {//下载完成后
                //同上处理
                imageV.image = image;
            }
        }];
    }

    imageV.clipsToBounds = YES;
    imageV.contentMode = UIViewContentModeScaleAspectFill;
    
    UILabel *titleLabel = [TomamaUI createLabel:CGRectMake(CGRectGetMaxX(imageV.frame) + 19*screenW_6, imageV.frame.origin.y, SCREENW - CGRectGetMaxX(imageV.frame) - 35*screenW_6, 17*screenW_6) textAligment:0 font:[UIFont systemFontOfSize:17*screenW_6] textColor:TextBlack text:title];
    [baseView addSubview:titleLabel];

    //星星
    _startView = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(imageV.frame)+19*screenW_6, CGRectGetMaxY(titleLabel.frame)+15*screenW_6, SCREENW-100*screenW_6, 30*screenW_6)];;
    [baseView addSubview:_startView];
    [self addStart:data];
    
    UIImageView *icon = [TomamaUI createImageView:CGRectMake(titleLabel.frame.origin.x+5*screenW_6, CGRectGetMaxY(_startView.frame) + 5*screenW_6, 9*screenW_6, 12*screenW_6) backGroundImageV:@"Main_navi"];
    [baseView addSubview:icon];
    UILabel *adressLabel = [TomamaUI createLabel:CGRectMake(CGRectGetMaxX(icon.frame) + 7*screenW_6, 0, SCREENW - CGRectGetMaxX(icon.frame) - 22*screenW_6, 13*screenW_6) textAligment:0 font:[UIFont systemFontOfSize:13*screenW_6] textColor:[UIColor colorWithHexString:@"#666666"] text:adress];
    [baseView addSubview:adressLabel];
    adressLabel.center = CGPointMake(adressLabel.center.x, icon.center.y);
    
    UILabel *moneyLabel = [TomamaUI createLabel:CGRectMake(titleLabel.frame.origin.x, CGRectGetMaxY(adressLabel.frame) + 20*screenW_6, [TomamaUI getStringWeithtWith:money andHeigth:17*screenW_6 andFont:[UIFont systemFontOfSize:17*screenW_6]], 17*screenW_6) textAligment:0 font:[UIFont systemFontOfSize:17*screenW_6] textColor:[UIColor colorWithHexString:@"#ff6900"] text:money];
    [baseView addSubview:moneyLabel];
    
    _xiaofeiLab =  [TomamaUI createLabel:CGRectMake(CGRectGetMidX(moneyLabel.frame)+10*screenW_6, CGRectGetMaxY(adressLabel.frame) + 24*screenW_6,SCREENW-CGRectGetMidX(moneyLabel.frame)-20*screenW_6,13*screenW_6) textAligment:2 font:[UIFont systemFontOfSize:12*screenW_6] textColor:TextGray text:[NSString stringWithFormat:@"%@已经消费",data[@"consumeNumber"]]];
    [baseView addSubview:_xiaofeiLab];
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 160*screenW_6 - .5, SCREENW, .5)];
    lineView.backgroundColor = backColor;
    [baseView addSubview:lineView];
}

-(void)addStart:(NSDictionary *)startDic
{
    for (UIImageView *subImageV in _startView.subviews) {
        [subImageV removeFromSuperview];
    }
    
    for (int i = 0; i < 5; i++) {
        
        float x =20*screenW_6*i+5*screenW_6;
        UIImageView *startImageV = [TomamaUI createImageView:CGRectMake(x,0,15*screenW_6,15*screenW_6) backGroundImageV:@"Mian_star_hui"];
//        if ([ToolViewVC isNotNullStr:startDic[@"evaluateLevel"]]) {
            if (i < [startDic[@"starClass"] integerValue]) {
                startImageV.image = [UIImage imageNamed:@"Main_star_select"];
            }else
            {
                startImageV.image = [UIImage imageNamed:@"Mian_star_hui"];
            }
//        }
        startImageV.tag = i+1;
        startImageV.userInteractionEnabled = NO;
        [_startView addSubview:startImageV];
    }
}

@end
